<?php

/*
*	$version: 2009-6-3 pm 12:14:45 beta 1.0
*	$id: SessionDb.class.php
*	$author: taoFeng
*	$mailto: font99@Gmail.com
*	$license: This is not a freeware
*/

if (!defined('IN_LEKO')) {header("HTTP/1.0 403 Forbidden");exit;}

class SessionDb
{
    /**
     * a database connection resource
     * @var resource
     */
    private $_sessLink;
	private $_storageObj;
	private $_lifeTime = 1440;
    /**
     * Open the session
     * @return bool
     */
    public function open() {

		$this->_lifeTime = ini_get('session.gc_maxlifetime');

		if ($GLOBALS['_G']['sessNewlink']) {
			$_tmp = new $GLOBALS['_G']['dbType'];
			$connect = $_tmp->stoageInit($GLOBALS['_G']['sessDbHost'], $GLOBALS['_G']['sessDbUser'], $GLOBALS['_G']['sessDbPass'], $GLOBALS['_G']['sessDbName'], $GLOBALS['_G']['sessDbPort'], false);
			$this->_sessLink = $_tmp->db_connect_id;
			$this->_storageObj = $_tmp;
			return $connect;
		}
		else {
			if (is_object($GLOBALS['_D'])) {
				$this->_sessLink = $GLOBALS['_D']->db_connect_id;
				$this->_storageObj = $GLOBALS['_D'];
				return true;
			}
		}
				
        return false;
    }

    /**
     * Close the session
     * @return bool
     */
    public function close() {
		$this->gc($this->_lifeTime); 
		$this->_storageObj->_dbClose();
    }

    /**
     * Read the session
     * @param int session id
     * @return string string of the sessoin
     */
    public function read($_sessId) {

		$_sessId = $this->_storageObj->Escape($_sessId);
		$sql = sprintf("SELECT `session_data` as d FROM `{$GLOBALS['_G']['tablePrefix']}sessions` " .
					   "WHERE `session_id` = '%s' and `session_expires` > '%s'", $_sessId,time());
		if ($result = $this->_storageObj->Query($sql)) {
			if ($this->_storageObj->getNumrows($result)) {
				$record = $this->_storageObj->fetchRow($result);
				return $record['d'];
			}
		}

        return '';

    }

    /**
     * Write the session
     * @param int session id
     * @param string data of the session
     */
    public function write($_sessId, $_sessData) {
		if (!empty($_sessData))
		{
			$sql = sprintf("REPLACE INTO `{$GLOBALS['_G']['tablePrefix']}sessions` VALUES('%s', '%s', '%s')",
						   $this->_storageObj->Escape($_sessId),
						   (time() + $this->_lifeTime),
						   $this->_storageObj->Escape($_sessData));
			return $this->_storageObj->Query($sql);
		}
    }

    /**
     * Destoroy the session
     * @param int session id
     * @return bool
     */
    public function destroy($_sessId) {
		$sql = sprintf("DELETE FROM `{$GLOBALS['_G']['tablePrefix']}sessions` WHERE `session_id` = '%s'", $_sessId);
		return $this->_storageObj->Query($sql);
}

    /**
     * Garbage Collector
     * @param int life time (sec.)
     * @return bool
     * @see session.gc_divisor      100
     * @see session.gc_maxlifetime 1440
     * @see session.gc_probability    1
     * @usage execution rate 1/100
     *        (session.gc_probability/session.gc_divisor)
     */
    public function gc($max) {
        $sql = sprintf("DELETE FROM `{$GLOBALS['_G']['tablePrefix']}sessions` WHERE `session_expires` < '%s'",
                       time());
		return $this->_storageObj->Query($sql);
    }
}

?>
